package com.itheima.dubbo.web.controller;

import com.itheima.dubbo.UserInterface;
import com.itheima.dubbo.pojo.User;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("user")
public class UserController {

//    @DubboReference(timeout = 1000, check = false)  // 消费服务  远程调用
//    @DubboReference(version = "1.0.1")
    @DubboReference(retries = 2, cluster = "failfast")
    UserInterface userInterface;


    @GetMapping("hello")
    public String sayHello(String username) {
        return userInterface.sayHello(username);
    }

    @GetMapping("findOne/{id}")
    public User findOne(@PathVariable("id") Long id) {

        // 本端是否为消费端，这里会返回true
//        boolean isConsumerSide = RpcContext.getContext().isConsumerSide();
        // 获取最后一次调用的提供方IP地址
//        String serverIP = RpcContext.getContext().getRemoteHost();
        // 获取当前服务配置信息，所有配置信息都将转换为URL的参数
//        String application = RpcContext.getContext().getUrl().getParameter("application");
//        URL url = RpcContext.getContext().getUrl();

        // 隐式传参
        RpcContext.getContext().setAttachment("company", "itheima");

        return userInterface.findOne(id);
    }
}
